﻿

package entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import management.SystemManagement;

/**
 *
 * @author hai
 */
public class Account {
    private int ID;
    private String accNo;
    private String PIN;
    private float balance;
    private boolean status;
    private float totalWithdrawAmountToday;
    private float totalDepositAmountToday;
    private int countWithdrawToday;
    private int countDepositToday;

    public static boolean changePIN(Account acc, String newPIN, Connection conn) throws SQLException {
        boolean b = false;
        if (conn != null) {
            PreparedStatement preStmt = conn.prepareCall("{call sp_change_pin(?,?)}");
            preStmt.setInt(1, acc.getID());
            preStmt.setString(2, newPIN);
            b =  preStmt.execute();
            preStmt.close();
        }
        return b;
    }

    public void getInfo(Connection conn) throws SQLException {
        if (conn != null) {
            PreparedStatement preStmt = conn.prepareCall("{call sp_check_account(?)}");
            preStmt.setInt(1, this.ID);
            ResultSet rs = preStmt.executeQuery();
            if(rs.next()) {
                countWithdrawToday = rs.getInt(1);
                countDepositToday = rs.getInt(2);
                totalWithdrawAmountToday =  rs.getFloat(3);
                totalDepositAmountToday = rs.getFloat(4);
                balance = rs.getFloat(5);
                status = rs.getBoolean(6);
                PIN = rs.getString(7);
            }
            preStmt.close();
            rs.close();
        }
    }

    public static String getMiniStatementAsHtmlString(Account acc, Connection conn, language.Language lang) throws SQLException {
        String str = "";
        if (conn != null) {
            PreparedStatement preStmt = conn.prepareCall("{call sp_view_10_last_tran(?)}");
            preStmt.setInt(1, acc.getID());
            ResultSet rs = preStmt.executeQuery();
            str += "<html><div style='text-align:center;font-weight:bold;margin:10;font-size:24'>";
            str += lang.getString("THÔNG TIN SAO KÊ");
            str += "</div><div><table width='500' cellpadding='3' cellspacing='3'><tbody>";
            str += "<th width='25%' style='background-color:#000033;color:#FFFFFF'>"
                    + lang.getString("Kiểu")
                    + "</th>";
            str += "<th witth='35%' style='background-color:#000033;color:#FFFFFF'>"
                    + lang.getString("Số lượng")
                    + "</th>";
            str += "<th witth='40%' style='background-color:#000033;color:#FFFFFF'>"
                    + lang.getString("Ngày")
                    + "</th>";
            while (rs.next()) {
                str += "<tr>";
                str += "<td>"
                            + (rs.getBoolean("Tran_Type")
                                    ? lang.getString("Rút tiền")
                                    : lang.getString("Gửi tiền"))
                            +"</td>";
                str += "<td style='text-align:right'>$"
                        + SystemManagement.convertMoney(rs.getFloat("Tran_Amount"))
                        + "</td>";
                str += "<td style='text-align:right'>" + rs.getString("Date") + "</td>";
            }
            preStmt.close();
            rs.close();
            str += "</tbody></table></div><div style='text-align:center;font-weight:bold'>";
            str += lang.getString("SỐ DƯ TÀI KHOẢN") + ": $" + SystemManagement.convertMoney(acc.getBalance());
            str += "</div></html>";
        }
        return str;
    }
    /**
     * @return the ID
     */
    public int getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(int ID) {
        this.ID = ID;
    }

    /**
     * @return the accNo
     */
    public String getAccNo() {
        return accNo;
    }

    /**
     * @param accNo the accNo to set
     */
    public void setAccNo(String accNo) {
        this.accNo = accNo;
    }

    /**
     * @return the PIN
     */
    public String getPIN() {
        return PIN;
    }

    /**
     * @param PIN the PIN to set
     */
    public void setPIN(String PIN) {
        this.PIN = PIN;
    }

    /**
     * @return the balance
     */
    public float getBalance() {
        return balance;
    }

    /**
     * @param balance the balance to set
     */
    public void setBalance(float balance) {
        this.balance = balance;
    }

    /**
     * @return the status
     */
    public boolean getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(boolean status) {
        this.status = status;
    }

    /**
     * @return the totalWithdrawAmountToday
     */
    public float getTotalWithdrawAmountToday() {
        return totalWithdrawAmountToday;
    }

    /**
     * @return the totalDepositAmountToday
     */
    public float getTotalDepositAmountToday() {
        return totalDepositAmountToday;
    }

    /**
     * @return the countWithdrawToday
     */
    public int getCountWithdrawToday() {
        return countWithdrawToday;
    }

    /**
     * @return the countDepositToday
     */
    public int getCountDepositToday() {
        return countDepositToday;
    }

}
